/*
	Copyright (c) 2009, M.P. Bartnig & A. Gosman
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
package nl.sanity.ui
{
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import nl.sanity.utils.MathUtils;


	public class Slider extends BaseComponent
	{
		public static const ORIENTATION_HORIZONTAL	: String 	= "orientationHorizontal";
		public static const ORIENTATION_VERTICAL	: String 	= "orientationVertical";
		
		public static const DEFAULT_MIN				: Number 	= 0;
		public static const DEFAULT_MAX				: Number 	= 100;
		
		protected var _track		: Button;
		protected var _thumb		: Button;
		
		protected var _orientation	: String;
		protected var _dragRatio	: Number;
		protected var _value		: Number;
		protected var _dragBounds	: Rectangle;
		protected var _min			: Number;
		protected var _max			: Number;
		
		
		public function Slider( parent:DisplayObjectContainer = null, posX:Number = 0, posY:Number = 0 )
		{
			super( parent, posX, posY );
		}
		
		
		// --- GETTERS/SETTERS -----------------------------------------
	
		public function get orientation():String
		{
			return _orientation;
		}
		
		public function set orientation( value:String ):void
		{
			_orientation	= value;
			
			invalidate();
		}
		
		public function get min():Number
		{
			return _min;
		}
		
		public function get max():Number
		{
			return _max;
		}
		
		public function get value():Number
		{
			return _value;
		}
		public function set value( value:Number ):void
		{
			_value	= value;
			
			invalidate();
		}
		
		override public function set enabled( value:Boolean ):void
		{
			super.enabled 		= value;
			
			_track.enabled		= value;
			_thumb.enabled		= value;
		}
		
		override public function set width( value:Number ):void
		{
			_track.width	= value;
			
			super.width 	= value;
		}
		
		
		// --- OVERRIDE FUNCTIONS -----------------------------------------

		override protected function init():void
		{
			super.init();
			
			_orientation 	= Slider.ORIENTATION_HORIZONTAL;
			_dragRatio		= 0.5;
			_min 			= Slider.DEFAULT_MIN;
			_max 			= Slider.DEFAULT_MAX;
		}
		
		override protected function addChildren():void
		{
			addTrack();
			addThumb();
		}
		
		override protected function addEventListeners():void
		{
			super.addEventListeners();
			
			_thumb.addEventListener( MouseEvent.MOUSE_DOWN, startDragging, false, 0, true );
			_track.addEventListener( MouseEvent.MOUSE_UP, onTrackClicked, false, 0, true );
		}
		
		override protected function removeEventListeners():void
		{
			super.removeEventListeners();
			
			_thumb.removeEventListener( MouseEvent.MOUSE_DOWN, startDragging );
			_track.removeEventListener( MouseEvent.MOUSE_UP, onTrackClicked );
		}
		
		override public function draw():void
		{
			alignChildren();
			setDragBounds();
			
			if( !_isDrawn )
			{
				updateThumbPosition();
			}
			
			super.draw();
		}
		
		
		// --- PUBLIC FUNCTIONS -----------------------------------------

		public function setThumbStateMap( bitmap:Bitmap, scale9Grid:Rectangle = null, smoothing:Boolean = false ):void
		{
			_thumb.setStateMap( bitmap, scale9Grid, smoothing );
			
			invalidate();
		}
		
		public function setTrackStateMap( bitmap:Bitmap, scale9Grid:Rectangle = null, smoothing:Boolean = false ):void
		{
			_track.setStateMap( bitmap, scale9Grid, smoothing );
			
			invalidate();
		}
		
		public function setRange( minValue:Number, maxValue:Number ):void
		{
			if( maxValue < minValue )
			{
				throw new IllegalOperationError( "Max value cannot be smaller than min value" );
			}
			
			_min 		= minValue;
			_max 		= maxValue;
			
			invalidate();
		}
		
		// --- PROTECTED FUNCTIONS -----------------------------------------
		
		protected function addTrack():void
		{
			_track 		= new Button( this, 0, 0, true );
		}
		
		protected function addThumb():void
		{
			_thumb 		= new Button( this, 0, 0, false );
		}
		
		protected function alignChildren():void
		{
			_thumb.y 	= 0.5 * _track.height - 0.5 * _thumb.height;
		}
		
		protected function setDragBounds():void
		{
			_dragBounds		= new Rectangle( _track.x, _thumb.y, _track.width - _thumb.width, 0 );
		}
		
		protected function startDragging( event:MouseEvent = null ):void
		{
			_thumb.startDrag( false, _dragBounds );
			
			stage.addEventListener( MouseEvent.MOUSE_UP, stopDragging, false, 0, true );
		}
		
		protected function stopDragging( event:MouseEvent = null ):void
		{
			_thumb.stopDrag();
			
			stage.removeEventListener( MouseEvent.MOUSE_UP, stopDragging );
			
			updateOnChange();
		}
		
		protected function updateOnChange():void
		{
			updateDragRatio();
			updateValue();
			
			dispatchEvent( new Event( Event.CHANGE ) );
		}
		
		protected function onTrackClicked( event:MouseEvent ):void
		{
			_thumb.x 	= MathUtils.confine( _track.mouseX - 0.5 * _thumb.width, _dragBounds.x, _dragBounds.x + _dragBounds.width );
			
			updateOnChange();
		}
		
		protected function updateDragRatio():void
		{
			_dragRatio	= _thumb.x / ( _dragBounds.width - _dragBounds.x );
		}
		
		protected function updateThumbPosition():void
		{
			_thumb.x 	= ( _dragRatio *  _dragBounds.width ) + _dragBounds.x;
		}
		
		protected function updateValue():void
		{
			_value 		= _dragRatio * ( _max - _min ) + _min;
		}
	}
}